/**
 * Created by wlc on 2018/4/19.
 */
$(document).ready(function(){
    //设置昵称悬停和焦点样式
    $("#username").focusout(function(){
        $("#username").css("background-color","white");
        $("#username-icon").css("color","#c8c8c8");
    });
    $("#username").hover(function(){
        $("#username").css("background-color","beige");
        $("#username-icon").css("color","#45D0E7");
    },function(){
        if (!$("#username").is(":focus")){
            $("#username").css("background-color","white");
            $("#username-icon").css("color","#c8c8c8");
        }
    });
    //设置邮箱悬停和焦点样式
    $("#email").focusout(function(){
        $("#email").css("background-color","white");
        $("#email-icon").css("color","#c8c8c8");
    });
    $("#email").hover(function(){
        $("#email").css("background-color","beige");
        $("#email-icon").css("color","#45D0E7");
    },function(){
        if (!$("#email").is(":focus")){
            $("#email").css("background-color","white");
            $("#email-icon").css("color","#c8c8c8");
        }
    });

    //设置密码悬停和焦点样式
    $("#password").focusout(function(){
        $("#password").css("background-color","white");
        $("#password-icon").css("color","#c8c8c8");
    });
    $("#password").hover(function(){
        $("#password").css("background-color","beige");
        $("#password-icon").css("color","#45D0E7");
    },function(){
        if (!$("#password").is(":focus")){
            $("#password").css("background-color","white");
            $("#password-icon").css("color","#c8c8c8");
        }
    });

    //设置密码确认悬停和焦点样式
    $("#password-verification").focusout(function(){
        $("#password-verification").css("background-color","white");
        $("#password-verification-icon").css("color","#c8c8c8");
    });
    $("#password-verification").hover(function(){
        $("#password-verification").css("background-color","beige");
        $("#password-verification-icon").css("color","#45D0E7");
    },function(){
        if (!$("#password-verification").is(":focus")){
            $("#password-verification").css("background-color","white");
            $("#password-verification-icon").css("color","#c8c8c8");
        }
    });

    //设置密码可见和不可见样式
    $("#password-icon").click(function () {
        if($("#password-icon")[0].className === "fa fa-lock fa-fw"){
            $("#password-icon")[0].className = "fa fa-unlock fa-fw";
            $("#password")[0].type="text";

            $("#password-verification-icon")[0].className = "fa fa-unlock fa-fw";
            $("#password-verification")[0].type = "text";
        }else {
            $("#password-icon")[0].className = "fa fa-lock fa-fw";
            $("#password")[0].type = "password";

            $("#password-verification-icon")[0].className = "fa fa-lock fa-fw";
            $("#password-verification")[0].type = "password";
        }
    });

    //设置通知栏
    notyf = new Notyf();
});

/**
 * 用户注册信息处理
 */
$(document).on("click",'#register-button', function(e){
    var username = trim($('#username')[0].value);
    var password = trim($('#password')[0].value);
    var password_confirm = trim($('#password-verification')[0].value);
    var mail = trim($('#email')[0].value);
    if (!isEmpty(username) && !isEmpty(password) && !isEmpty(mail)){
        if (mail.indexOf("@") !== -1){
            if (password === password_confirm){
                $.ajax({
                    type: 'POST',
                    url: '../home/register_userexist',
                    data: {username:username},
                    success: function (data) {
                        if (data === "true"){
                            notyf.alert('用户名已存在！');
                        }else {
                            document.getElementById("register-submit").click();
                            notyf.confirm('注册成功！');
                        }
                    }
                });
            }else {
                notyf.alert('密码不相同！');
            }
        }else {
            notyf.alert('邮箱格式不对！');
        }
    }else {
        notyf.alert('注册信息不能为空！');
    }
});